﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Forms;
using DAQ.Common;
using DAQ.PROTOCOL;
using System.Threading;

namespace DAQ
{
    public partial class Viewers : MetroFramework.Controls.MetroUserControl
    {
        public bool EnableRead { get => enableRead; set => enableRead = value; }
        private bool enableRead = true;
        public Viewers()
        {
            InitializeComponent();
            Task.Factory.StartNew(() =>
            {
                PC_LINK ta1;
                PC_LINK ta2;
                PC_LINK ta3;
                PC_LINK ta4;
                PC_LINK ta5;
                PC_LINK ta6;
                while (true)
                {
                    try
                    {
                        var array = Config.GetAll().Where(t => t.Key.Contains("MAINTENSION")).ToArray();
                        int persent = int.Parse(Config.GetConfig(CFGS.TENSIONS, CFGS.TENSION_ERROR));
                        if (EnableRead)
                        {

                            ta1 = CWMCmn.Inst.GetPC_LINK(1);
                            if (ta1 != null)
                            {
                                btsR_PCLINK1.pc_link = ta1;
                                int upper = ta1.WORK_TENSION * (1 + persent / 100);
                                int lower = ta1.WORK_TENSION * (1 - persent/ 100);
                                CWMCmn.Inst.DataStore.HoldingRegisters[1] = (ushort)(ta1.TENSION_DISPLAY_AVERAGE & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[2] = (ushort)((ta1.TENSION_DISPLAY_AVERAGE >> 16) & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[3] = (ushort)(upper & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[4] = (ushort)((upper >> 16) & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[5] = (ushort)(lower & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[6] = (ushort)((lower >> 16) & 0xffff);
                            }
                            Thread.Sleep(100);
                            ta2 = CWMCmn.Inst.GetPC_LINK(2);
                            if (ta2 != null)
                            {
                                int upper = ta2.WORK_TENSION * (1 + persent/ 100);
                                int lower = ta2.WORK_TENSION * (1 - persent / 100);
                                CWMCmn.Inst.DataStore.HoldingRegisters[7] = (ushort)(ta2.TENSION_DISPLAY_AVERAGE & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[8] = (ushort)((ta2.TENSION_DISPLAY_AVERAGE >> 16) & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[9] = (ushort)(upper & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[10] = (ushort)((upper >> 16) & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[11] = (ushort)(lower & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[12] = (ushort)((lower >> 16) & 0xffff);
                                btsR_PCLINK2.pc_link = ta2;
                            }
                            Thread.Sleep(100);
                            ta3 = CWMCmn.Inst.GetPC_LINK(3);
                            if (ta3 != null)
                            {
                                int upper = ta3.WORK_TENSION * (1 + persent/ 100);
                                int lower = ta3.WORK_TENSION * (1 - persent/ 100);
                                CWMCmn.Inst.DataStore.HoldingRegisters[13] = (ushort)(ta3.TENSION_DISPLAY_AVERAGE & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[14] = (ushort)((ta3.TENSION_DISPLAY_AVERAGE >> 16) & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[15] = (ushort)(upper & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[16] = (ushort)((upper >> 16) & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[17] = (ushort)(lower & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[18] = (ushort)((lower >> 16) & 0xffff);
                                btsR_PCLINK3.pc_link = ta3;
                            }
                            Thread.Sleep(100);
                            ta4 = CWMCmn.Inst.GetPC_LINK(4);
                            if (ta4 != null)
                            {
                                int upper = ta4.WORK_TENSION * (1 + persent / 100);
                                int lower = ta4.WORK_TENSION * (1 - persent/ 100);
                                CWMCmn.Inst.DataStore.HoldingRegisters[19] = (ushort)(ta4.TENSION_DISPLAY_AVERAGE & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[20] = (ushort)((ta4.TENSION_DISPLAY_AVERAGE >> 16) & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[21] = (ushort)(upper & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[22] = (ushort)((upper >> 16) & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[23] = (ushort)(lower & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[24] = (ushort)((lower >> 16) & 0xffff);
                                btsR_PCLINK4.pc_link = ta4;
                            }
                            Thread.Sleep(100);

                            ta5 = CWMCmn.Inst.GetPC_LINK(5);
                            if (ta5 != null)
                            {
                                int upper = ta5.WORK_TENSION * (1 + persent/ 100);
                                int lower = ta5.WORK_TENSION * (1 - persent/ 100);
                                CWMCmn.Inst.DataStore.HoldingRegisters[25] = (ushort)(ta5.TENSION_DISPLAY_AVERAGE & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[26] = (ushort)((ta5.TENSION_DISPLAY_AVERAGE >> 16) & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[27] = (ushort)(upper & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[28] = (ushort)((upper >> 16) & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[29] = (ushort)(lower & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[30] = (ushort)((lower >> 16) & 0xffff);
                                btsR_PCLINK5.pc_link = ta5;
                            }
                            Thread.Sleep(100);

                            ta6 = CWMCmn.Inst.GetPC_LINK(6);
                            if (ta6 != null)
                            {
                                int upper = ta6.WORK_TENSION * (1 + persent/ 100);
                                int lower = ta6.WORK_TENSION * (1 - persent/ 100);
                                CWMCmn.Inst.DataStore.HoldingRegisters[31] = (ushort)(ta6.TENSION_DISPLAY_AVERAGE & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[32] = (ushort)((ta6.TENSION_DISPLAY_AVERAGE >> 16) & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[33] = (ushort)(upper & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[34] = (ushort)((upper >> 16) & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[35] = (ushort)(lower & 0xffff);
                                CWMCmn.Inst.DataStore.HoldingRegisters[36] = (ushort)((lower >> 16) & 0xffff);
                                btsR_PCLINK6.pc_link = ta6;
                            }
                            Thread.Sleep(100);
                        }
                    }
                    catch (Exception ex)
                    {
                     //  MessageBox.Show(ex.Message);
                     //   enableRead = false;
                    }
                }
            });           
        }

        private void btsR_PCLINK5_Load(object sender, EventArgs e)
        {

        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
